package yxw.com.smartbutler.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import yxw.com.smartbutler.R;
import yxw.com.smartbutler.entity.MyUser;

/*
 *  项目名： SmartButler
 *  包名:    yxw.com.smartbutler.ui
 *  文件名： ForgetPwdActivity
 *  创建者：小威.岳
 *  创建时间：2018/11/14 17:04
 *  描述： TODO
 */public class ForgetPwdActivity extends AppCompatActivity implements View.OnClickListener {

    //忘记密码
    private Button btn_forget_pwd;
    private EditText et_email;

    private EditText et_old_pwd;
    private EditText et_new_pwd;
    private EditText et_renew_pwd;
    private Button btn_update_pwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpwd);
        initView();
    }

    private void initView() {
        btn_forget_pwd = findViewById(R.id.btn_forget_pwd);
        btn_forget_pwd.setOnClickListener(this);

        et_email = findViewById(R.id.et_email);
        et_new_pwd = findViewById(R.id.et_new_pwd);
        et_old_pwd = findViewById(R.id.et_old_pwd);
        et_renew_pwd = findViewById(R.id.et_renew_pwd);
        btn_update_pwd = findViewById(R.id.btn_update_pwd);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_forget_pwd:
                //获取输入框的邮箱
                final String email = et_email.getText().toString().trim();
                //判断是否为空
                if (!TextUtils.isEmpty(email)) {
//发送邮件
                    MyUser.resetPasswordByEmail(email, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                Toast.makeText(ForgetPwdActivity.this, "邮箱已经发送至：" + email, Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(ForgetPwdActivity.this, "邮箱发送失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } else {
                    Toast.makeText(ForgetPwdActivity.this, "邮箱不能为空", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.btn_update_pwd:
                //获取输入框的值
                String oldPwd = et_old_pwd.getText().toString().trim();
                String newPwd = et_new_pwd.getText().toString().trim();
                String renewPwd = et_renew_pwd.getText().toString().trim();

                if (!TextUtils.isEmpty(oldPwd) & !TextUtils.isEmpty(newPwd) & !TextUtils.isEmpty(renewPwd)) {
                    if (newPwd.equals(renewPwd)) {
                        MyUser.updateCurrentUserPassword(oldPwd, newPwd, new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e == null) {
                                    Toast.makeText(ForgetPwdActivity.this, "密码重置成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(ForgetPwdActivity.this, "密码重置失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    } else {
                        Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
